package itsos.controller;

import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import itsos.service.*;
import java.util.*;
import itsos.common.*;
import itsos.model.*;

public class AdminController extends MultiActionController {
	
	private RepositoryService repositoryService;
	private MainIssueService mainIssueService;
	private MemberService memberService;
	private IssueBoxService issueBoxService;
	

	
	public void setIssueBoxService(IssueBoxService issueBoxService) {
		this.issueBoxService = issueBoxService;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public void setMainIssueService(MainIssueService mainIssueService)
	{
		this.mainIssueService = mainIssueService;
	}
	
	public void setRepositoryService(RepositoryService repositoryService)
	{
		this.repositoryService = repositoryService;
	}
	
	public ModelAndView superLogin(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView mav = new ModelAndView("workpage/admin/sltpage");
		if(!request.getParameter("adminID").equals(ConstCode.SUPER_ADMIN_ID))
		{
			mav.setViewName("workpage/mainissue/introPage");
		}
		if(!request.getParameter("adminPassword").equals(ConstCode.SUPER_ADMIN_PASS))
		{
			mav.setViewName("workpage/mainissue/introPage");
		}
		
		
		
		if(request.getSession().getAttribute("memberID")!=null)
			mav.addObject("issueBox", issueBoxService.getIssueBox(request));
		mav.addObject("treeView", repositoryService.getRepository());
		return mav;
	}
	
	
	public ModelAndView adminIntro(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView mav = new ModelAndView("workpage/admin/adminIntro");
		
		
		if(request.getSession().getAttribute("memberID")!=null)
			mav.addObject("issueBox", issueBoxService.getIssueBox(request));
		mav.addObject("treeView", repositoryService.getRepository());
		
		return mav;
	}
	

	public ModelAndView selectTree(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView mav = new ModelAndView("workpage/admin/selectTree");
		List list;
		list = repositoryService.searchRepositoryTree(request);
		mav.addObject("repositoryTree", list);
		
		
		if(request.getSession().getAttribute("memberID")!=null)
			mav.addObject("issueBox", issueBoxService.getIssueBox(request));
		mav.addObject("treeView", repositoryService.getRepository());
		
		return mav;
	}
	
	public ModelAndView selectRepository(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView mav = new ModelAndView("workpage/admin/insertRepository");
		if(new Long(Long.parseLong(request.getParameter("tree"))).equals(ConstCode.TRUNK))
		{
			mav.setViewName("workpage/admin/noTrunk");
			return mav;
		}else if(new Long(Long.parseLong(request.getParameter("tree"))).equals(ConstCode.BRANCH))
		{
			mav.addObject("tree", ConstCode.BRANCH);
		}else if(new Long(Long.parseLong(request.getParameter("tree"))).equals(ConstCode.TAGS))
		{
			mav.addObject("tree", ConstCode.TAGS);
		}
		
		
		List list, list2, list3 = new ArrayList();
		Iterator i,x;
		
		list = repositoryService.searchRepositoryTree2(request);
	//	mav.addObject("repositoryTree", list);
		
		list2 = repositoryService.getRepository(new Long(Long.parseLong(request.getParameter("tree"))));
		i = list.iterator();
		
		Repository repository =new Repository();
		String s;
		
		while(i.hasNext())
		{
			int f=0;
			s = (String)i.next();
			x = list2.iterator();
			while(x.hasNext())
			{
				repository = (Repository) x.next();
				if(s.equals(repository.getRevisionName()))
				{
					f=1;
				}
			}
			if(f==0)
			{
				list3.add(s);
			}
			
		}
		mav.addObject("repositoryTree", list3);
		mav.addObject("dbRepository", list2);
		
		
		if(request.getSession().getAttribute("memberID")!=null)
			mav.addObject("issueBox", issueBoxService.getIssueBox(request));
		mav.addObject("treeView", repositoryService.getRepository());
		
		
		
		return mav;
	}
	
	public ModelAndView insertRepository(HttpServletRequest request, HttpServletResponse response)
	{
		Repository repository = new Repository();
		Repository repository2 = new Repository();
		
		
		ModelAndView mav = new ModelAndView("workpage/admin/insertMainIssue");
		repositoryService.insertRepository(request);
		repository = repositoryService.getRepositoryByRevision(request.getParameter("revision"));
		repository2 = repositoryService.getRepositoryByRevision(request.getParameter("beforeRepositoryName"));
		
		mav.addObject("repository", repository);
		mav.addObject("beforeRepository", repository2);
		
		
		if(request.getSession().getAttribute("memberID")!=null)
			mav.addObject("issueBox", issueBoxService.getIssueBox(request));
		mav.addObject("treeView", repositoryService.getRepository());
		
		return mav;
	}
	
	public ModelAndView comInsert(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView mav = new ModelAndView("workpage/admin/comMainIssue");
		
		mainIssueService.insertMainIssue(request);
		
		if(request.getSession().getAttribute("memberID")!=null)
			mav.addObject("issueBox", issueBoxService.getIssueBox(request));
		mav.addObject("treeView", repositoryService.getRepository());
		
		return mav;
	}
	
	
	
	
	public ModelAndView insertBranch(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView mav = new ModelAndView("workpage/admin/addMainIssue");
		List list1;
		list1 = repositoryService.searchRepositoryTree2(request);
		mav.addObject("repositoryTree", list1);
		
		
		if(request.getSession().getAttribute("memberID")!=null)
			mav.addObject("issueBox", issueBoxService.getIssueBox(request));
		mav.addObject("treeView", repositoryService.getRepository());
		
		return mav;
	}
	
	
	public ModelAndView member(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView mav = new ModelAndView("workpage/admin/setMember");
	
		mav.addObject("memberList", memberService.getLoginID());
		
		
		
	
		
		if(request.getSession().getAttribute("memberID")!=null)
			mav.addObject("issueBox", issueBoxService.getIssueBox(request));
		mav.addObject("treeView", repositoryService.getRepository());
				
		return mav;
	}
	
	public ModelAndView saveGroup(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView mav = new ModelAndView("workpage/admin/setMember");
		memberService.saveGroup(request);
		
		mav.addObject("memberList", memberService.getLoginID());
		
		
		if(request.getSession().getAttribute("memberID")!=null)
			mav.addObject("issueBox", issueBoxService.getIssueBox(request));
		mav.addObject("treeView", repositoryService.getRepository());
		
		return mav;
	}
	
	public ModelAndView selectMainIssueMember(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView mav = new ModelAndView("workpage/admin/mainIssueAdminSelect");
		
		mav.addObject("admin", memberService.selectAdmin());
		
		mav.addObject("mainIssue", mainIssueService.selectAdmin());
		
		
		if(request.getSession().getAttribute("memberID")!=null)
			mav.addObject("issueBox", issueBoxService.getIssueBox(request));
		mav.addObject("treeView", repositoryService.getRepository());
		return mav;
	}
	
	public ModelAndView selectedAdmin(HttpServletRequest request, HttpServletResponse response)
	{
		
		ModelAndView mav = new ModelAndView("workpage/admin/mainIssueAdminSelect");
		
		mainIssueService.adminUpdate(request);
		
		mav.addObject("admin", memberService.selectAdmin());
		mav.addObject("mainIssue", mainIssueService.selectAdmin());
		
		
		if(request.getSession().getAttribute("memberID")!=null)
			mav.addObject("issueBox", issueBoxService.getIssueBox(request));
		mav.addObject("treeView", repositoryService.getRepository());
		return mav;
		
		
	}
	

	
}
